package com.geek.gkfgw.integration.gkfood;

import com.geek.gkfgw.integration.gkfood.bean.DiscountItemBean;
import com.geek.gkfgw.integration.gkfood.bean.SnatchOrderReqBean;
import com.geek.gkfgw.integration.gkfood.bean.SnatchOrderRespBean;
import com.geek.gkfgw.util.consts.SystemConsts;
import com.geek.gkfgw.util.copy.ShallowCopyUtils;
import com.geek.gkfgw.util.excp.GkFgwCodeEnum;
import com.geek.gkfgw.util.excp.GkFgwException;
import com.geek.gkfgw.util.net.IpUtils;
import com.geek.gkfood.facade.meal.MealBookFacade;
import com.geek.gkfood.facade.meal.bean.DishesItem;
import com.geek.gkfood.facade.meal.bean.SnatchOrderReq;
import com.geek.gkfood.facade.meal.bean.SnatchOrderResp;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import java.util.ArrayList;

/**
 * <h1>餐饮预定服务远程调用客户端类。</h1><br/>
 *
 * <h1><br/><a href="https://u.geekbang.org/subject/arch3/100614201">Java 业务架构实战营：https://u.geekbang.org/subject/arch3/100614201</a></h1><br/><h1><a href="https://gitee.com/ylimhhmily/BizArchPracticeCamp">Java 业务架构实战营代码地址：https://gitee.com/ylimhhmily/BizArchPracticeCamp</a></h1><br/><h1><a href="https://u.geekbang.org/subject/java4th/1001148?source=app_share">高级 Java 工程体系课 2023 版：https://u.geekbang.org/subject/java4th/1001148?source=app_share</a></h1><br/><h1><a href="https://time.geekbang.org/column/intro/100312101?tab=catalog">Dubbo 源码剖析与实战：https://time.geekbang.org/column/intro/100312101?tab=catalog</a></h1>
 *
 * @author hmilyylimh
 * ^_^
 * @version 0.0.1
 * ^_^
 * @date 2023/9/28 05:44
 */
@Component
public class MealBookFacadeClient {

    @DubboReference
    private MealBookFacade mealBookFacade;

    /**
     * <h2>抢单下单。</h2>
     *
     * @param: req
     * @return: java.lang.String
     */
    public SnatchOrderRespBean snatchOrder(SnatchOrderReqBean req) {
        // 构建请求对象
        SnatchOrderReq newReq = buildSnatchOrderReq(req);

        // 执行调用
        SnatchOrderResp resp = mealBookFacade.snatchOrder(newReq);

        // 解析响应
        if (resp == null) {
            throw new GkFgwException(GkFgwCodeEnum.INVOKE_INTEGRATION_EXCEPTION);
        }
        if (GkFgwCodeEnum.isFail(resp.getRespCode())) {
            throw new GkFgwException(resp.getRespCode(), resp.getRespMsg());
        }
        return buildSnatchOrderResp(resp);
    }

    private SnatchOrderRespBean buildSnatchOrderResp(SnatchOrderResp resp) {
        SnatchOrderRespBean respBean = new SnatchOrderRespBean();

        respBean.setOrderId(resp.getOrderId());
        respBean.setTotalAmt(resp.getTotalAmt());
        respBean.setPayAmt(resp.getPayAmt());
        respBean.setDiscountDetailList(ShallowCopyUtils.copyListBeanPropertiesToList(resp.getDiscountDetailList(), new ArrayList<>(), DiscountItemBean.class));

        return respBean;
    }

    private static SnatchOrderReq buildSnatchOrderReq(SnatchOrderReqBean req) {
        SnatchOrderReq newReq = new SnatchOrderReq();
        newReq.setShopId(req.getShopId());
        newReq.setCurrUserId(req.getCurrUserId());
        newReq.setCurrUserName(req.getCurrUserName());
        newReq.setSelectedDishesList(ShallowCopyUtils.copyListBeanPropertiesToList(req.getSelectedDishesList(), new ArrayList<>(), DishesItem.class));
        newReq.setContactPhone(req.getContactPhone());

        newReq.setFromSystem(SystemConsts.SYS_NAME);
        newReq.setClientIp(IpUtils.getIp());
        return newReq;
    }
}